﻿using EasyTask.EasyHelper;
using EasyTask.EasyService;
using EasyTask.Models;
using SQLBuilder.Repositories;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyTaskUI
{
    public partial class FrmEdit : Form
    {
        SqliteRepository db = new SqliteRepository(ConnstringConfig.connStr);


        public string JobName { get; set; }
        public FrmEdit()
        {
            InitializeComponent();

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Tasks entity = new Tasks
            {
                JobName = JobName,
                Cron = txtCron.Text,
                DisplayJobName = txtDisplayJobName.Text,
                DisplayStateDesc = cboState.Text,
            };
            var statebox = GetCombox();
            if (statebox != "")
            {
                entity.State = Convert.ToInt32(statebox);
            }
            db.Update(entity);
            this.Close();
        }

        ///// <summary>
        ///// 准备运行
        ///// </summary>
        //[Description("准备运行")]
        //PrepareStart = 0,
        ///// <summary>
        ///// 已经运行
        ///// </summary>
        //[Description("已经运行")]
        //Started = 1,
        ///// <summary>
        ///// 准备停止
        ///// </summary>
        //[Description("准备停止")]
        //PrepareStop = 2,
        ///// <summary>
        ///// 已经停止
        ///// </summary>
        //[Description("已经停止")]
        //Stoped = -2,
        ///// <summary>
        ///// 准备暂停
        ///// </summary>
        //[Description("准备暂停")]
        //PreparePasue = 3,
        ///// <summary>
        ///// 已经暂停
        ///// </summary>
        //[Description("已经暂停")]
        //Paused = -3,
        ///// <summary>
        ///// 准备更新
        ///// </summary>
        //[Description("准备更新")]
        //PrepareUpdate = 4,
        ///// <summary>
        ///// 手动从服务中停止
        ///// </summary>
        //[Description("手动从服务中停止")]
        //ManualStoped = -9,

        ///// <summary>
        ///// 未知状态
        ///// </summary>
        //[Description("未知状态")]
        //Unknow = -8,

        ///// <summary>
        ///// 无效状态
        ///// </summary>
        //[Description("无效状态")]
        //Invalid = -7,

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            this.txtJobName.Text = JobName;
            var entity = db.FindEntity<Tasks>(JobName);
            txtDisplayJobName.Text = entity.DisplayJobName;
            txtCron.Text = entity.Cron;
            cbManualStopIsStart.Checked = entity.ManualStopIsStart == 1;
            SetCombox();
            SetCombox(EnumHelper.GetDescription(entity.ServiceState));
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExpressCron_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show(CronHelper.GetCronDescription(txtCron.Text)) ;
        }
        private void SetCombox(string display="")
        {
            if (display=="")
            {
                var s = new string[] { "准备运行", "准备停止", "准备暂停", "准备更新", "无效状态" };
                cboState.Items.AddRange(s);
            }
            else
            {
                cboState.SelectedText = display;
            }
        }
        private string GetCombox()
        {
            switch (cboState.Text)
            {
                case "准备运行": return "0";
                case "准备停止": return "2";
                case "准备暂停": return "3";
                case "准备更新": return "4";
                default:
                    break;
            }
            return "";
        }
    }
}
